import pagination from '@/views/pagination'  //分页组件
import agentHttp from '@/utils/agentHttp.js'//引用JS
import exportFile from '@/utils/exportFile.js'//引用JS
import {debounce,setEndDateTime} from '@/utils/index.js'
import dialogForm from './view'
import { parseTime } from 'utils'
export default {
  name: 'accountsReceivable',//组件名称
  components:{dialogForm,pagination},
  created(){//初始化
    this.seachData();
    this.getCarOrderStatusMap();
    this.allButton = buttonPermission('all');
    this.exportButton= buttonPermission('export');
    this.viewButton= buttonPermission('view');
  },
  data() {//初始化数据
    let that = this;
  	return {
  	  //弹窗状态数据
      dialogSendData:{
        isAdd:"",
        dialogFormVisible:"",
        id:""
      },
      //表格字段
      tableData:{	         
      },
      //搜索字段
  		searchForm:{	  			
	        startDate:"",
	        endDate:"",
	        status:"",
	        isOver:"",
          isInterest:"",
          expectedStartDate:"",
          expectedEndDate:""
  		},
      carOrderStatusMap:'',
      startDateOpt: {
        disabledDate(time) {
          return time.getTime() > Date.now() 
        }
      },
      endDateOpt:{
        disabledDate:(time)=>{
          return  time.getTime()< new Date(this.searchForm.startDate).getTime();
        }
      },
      endDateOpt1:{
        disabledDate:(time)=>{
          return  time.getTime()< new Date(this.searchForm.expectedStartDate).getTime();
        }
      },
      resetFormStatus:false,
      multipleSelection:[]
  	}
  },
  methods: {//所有方法
  	handleSee(index, row){
      row?this.dialogSendData.id=row.id:this.dialogSendData.id="";
      this.dialogSendData.isAdd=false;
      this.dialogSendData.dialogFormVisible=true;
    },
  	//数据流方式导出
  	exportForm(){
  		var params={};
      //var params=this.searchForm||{};
			//params.endDate = setEndDateTime(params.endDate);
      //params.expectedEndDate = setEndDateTime(params.expectedEndDate);
			agentHttp.report('adminApi/admin/accounts_receivable_report/export_excel',params ).then((data)=>{
			  exportFile('应收账款','xls',data.data);
		  })
  	},
    //用原生js做ajax请求
    doExport(){
      var xhr = new XMLHttpRequest();
      xhr.open("post", "adminApi/admin/accounts_receivable_report/export_excel", true);
      xhr.responseType = "blob";
      xhr.onload = function() {
        if(this.status == 200) {
          var blob = this.response;
          if(blob.size != 0){
            var objectURL = window.URL.createObjectURL(blob);
            location.href = objectURL;
          }
        }
      }
      xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xhr.send("");
    },

    //前端生成excel数据
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'orderRemitStartTime'||j === 'expectedFinishDate'||j === 'nowDate') {
          return parseTime(v[j])
        }else if(j==='status'){
          return this.carOrderStatusMap[v[j]];
        } 
        else {
          return v[j]
        }
      }))
    },
    handleDownload() {
      if (this.multipleSelection.length) {
        require.ensure([], () => {
          const { export_json_to_excel } = require('vendor/Export2Excel')
          const tHeader = ['订单号', '资金方', '经销商', '业务员', '汽车车型','车架号','打款日期','垫资周期','预计到期日','截止日期','总天数',
          '系统计算服务费天数','服务费是否异常','是否逾期','逾期天数','采购总价格','定金金额','垫资金额','保证金','服务费比率','服务费2比率',
          '服务费','服务费2','应收款金额','已回款金额','待回款金额','订单状态']
          const filterVal = ['orderNumber', 'funderName', 'dealerName', 'businessName', 'carName','vin', 'orderRemitStartTime', 'maximum', 'expectedFinishDate', 'nowDate'
          , 'sumDay', 'interestDay', 'isInterest', 'isOver', 'overDay', 'totalPrice', 'earnest', 'paymentAmount','deposit','interestRate','overInterestRate','interest','exceed',
          'allPrice','totalReturnPrice','returnMoney','status']
          const list = this.multipleSelection
          const data = this.formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '列表excel')
        })
      } else {
        this.$message({
          message: '至少导出一条数据',
          type: 'warning'
        })
      }
    },
    //前端生成excel数据完毕
    //
    //列表查询
    seachData(){
      if(!this.resetFormStatus){
        var params=this.searchForm||{};
        params.page=this.tableData.page||1;
        params.rows=this.tableData.rows||10;
        params.endDate = setEndDateTime(params.endDate);
        params.expectedEndDate = setEndDateTime(params.expectedEndDate);
        agentHttp.postJson('adminApi/admin/accounts_receivable_report/queryAllByEntity?page='+params.page+'&rows='+params.rows,params ).then((data)=>{
          this.tableData = data
          this.multipleSelection=data.data;
        })
      }
    },
    //延迟搜索
    debounceSeach(){
      this.tableData.page=1;
      debounce(this.seachData(),1000)
    },
    //重置
    resetSearchForm(formName) {
      this.resetFormStatus=true;
      this.tableData.page=1;
      this.tableData.rows=10;
      this.$refs[formName].resetFields();
      this.$nextTick(_ =>{
        this.resetFormStatus=false;
        this.seachData();
      })
    },
    //获取订单状态
    getCarOrderStatusMap(){
      agentHttp.get('adminApi/admin/accounts_receivable_report/get_car_order_status_map' ).then((data)=>{
        if (data.success) {
          this.carOrderStatusMap = data.data;  
        }        
      })  
    }    
  }
}